package com.wander.concurrent;

import java.util.concurrent.*;

public class ThreadPoolExecutorExample {
    private static final int CORE_POOL_SIZE = 5;
    private static final int MAXIMUM_POOL_SIZE = 10;
    private static final int KEEP_ALIVE_TIME = 30;
    private static TimeUnit UNIT = TimeUnit.SECONDS;
    private static BlockingQueue<Runnable> QUEUE = new LinkedBlockingQueue<Runnable>(100);


    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, UNIT, QUEUE);
        for (int i = 0; i < 5; i++) {
            Future<?> future = executor.submit(() -> {
                //任务逻辑
                try {
                    System.out.println("do task...");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        try {
            countDownLatch.await();
            System.out.println("所有任务已完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
